import 'package:flutter/cupertino.dart' show CupertinoPageRoute;
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:path_provider/path_provider.dart';
import 'package:universal_io/io.dart';

BuildContext? partiallyPageContext;

///收起键盘
unfocus(BuildContext context) {
  FocusScope.of(context).unfocus();
}

///收起键盘
requestFocus(BuildContext context, {FocusNode? focusNode}) {
  if (focusNode != null) {
    FocusScope.of(context).requestFocus(focusNode);
  } else {
    FocusScope.of(context).requestFocus();
  }
}

///获取应用程序目录
Future<Directory> getApplicationDocumentsDir() =>
    getApplicationDocumentsDirectory();

/////////GetX方法/////////

///返回
void back<T>({
  T? result,
  bool closeOverlays = false,
  bool canPop = true,
  int? id,
  BuildContext? context,
}) {
  if (context != null) {
    Navigator.of(context).pop(result);
    return;
  }
  Get.back(result: result, closeOverlays: closeOverlays, canPop: canPop);
}

///进入页面
Future<T?>? goto<T>(
  dynamic page, {
  dynamic arguments,
  Transition? transition,
}) {
  return Get.to(page,
      arguments: arguments, transition: transition ?? Transition.cupertino);
}

///进入局部页面
Future<T?>? gotoPartially<T>(
  dynamic page, {
  required BuildContext context,
  Transition? transition,
}) {
  // Navigator.of(context).push(MaterialPageRoute(builder: (context) {
  //   return DetailPage();
  // }));
  partiallyPageContext = context;
  return Navigator.of(context).push(
    CupertinoPageRoute(builder: (context) {
      return page;
    }),
  );
}

///进入页面并关闭当前页面
Future<T?>? gotoAndOff<T>(
  dynamic page, {
  dynamic arguments,
  Transition? transition,
}) {
  Get.off(page,
      arguments: arguments, transition: transition ?? Transition.cupertino);
}

///进入页面并关闭之前所有页面
void gotoAndOffAll<T>(
  dynamic page, {
  dynamic arguments,
  Transition? transition,
}) {
  Get.offAll(page,
      arguments: arguments, transition: transition ?? Transition.cupertino);
}

///进入页面
Future<T?>? gotoNamed<T>(
  String pageName, {
  dynamic arguments,
}) {
  return Get.toNamed(pageName, arguments: arguments);
}

///进入页面并关闭之前所有页面
void gotoNamedAndOffAll(
  String pageName, {
  dynamic arguments,
}) {
  Get.offAllNamed(pageName, arguments: arguments);
}

/// 返回到指定页面
void backUntil(String pageName) {
  Get.until((route) => route.settings.name == pageName);
}

///返回到首页
void backToHome() {
  Get.until((route) => route.isFirst);
}

///打开对话框
Future<T?> openDialog<T>(
  Widget widget, {
  Object? arguments,
  Color? barrierColor,
  Duration? transitionDuration,
  bool barrierDismissible = true,
}) {
  return Get.dialog<T>(widget,
      arguments: arguments,
      barrierColor: barrierColor ?? Colors.black12,
      transitionDuration: transitionDuration,
      barrierDismissible: barrierDismissible);
}

Future<T?> openBottomSheet<T>(
  Widget widget, {
  bool isDismissible = true,
  bool isScrollControlled = false,
}) {
  return Get.bottomSheet(widget,
      isDismissible: isDismissible, isScrollControlled: isScrollControlled);
}

///强制更新APP所有小部件
Future<void> forceAppUpdate() {
  return Get.forceAppUpdate();
}
